// +build ignore

package main

import (
	"code.google.com/p/go8/chip8"
	"code.google.com/p/go8/games"
	"fmt"
	"math/rand"
	"time"
)

func main() {
	runner := chip8.NewRunner(
		*games.MostDangerousGame.FillEmptyData(),
		tickerFactory{},
	)

	chip := runner.Interpreter()
	chip.SetKeyEvent(randomKeyPress{})
	chip.SetKeyboard(randomlyDown{})
	randomizer := randomNumber{}
	randomizer.SeedWithNow()
	chip.SetRandomizer(randomizer)

	quit := make(chan bool)
	runner.Run(quit)

	drawTick := time.NewTicker(time.Millisecond * 500)
	defer drawTick.Stop()
	go func() {
		for _ = range drawTick.C {
			draw(chip.Screen())
		}
	}()

	fmt.Scanln()
	quit <- true
}

func draw(screen chip8.Screen) {
	w, h := screen.Size()
	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			pixel := "-"
			if screen.IsSet(x, y) {
				pixel = "x"
			}
			fmt.Print(pixel)
		}
		fmt.Println()
	}
	fmt.Println()
}

type alwaysKey0 struct{}

func (_ alwaysKey0) LastKey() (bool, uint8) { return true, 0 }

type randomKeyPress struct{}

func (_ randomKeyPress) LastKey() (bool, uint8) {
	return rand.Intn(10) == 0, uint8(rand.Intn(16))
}

func (_ randomKeyPress) ClearLastKey() {}

type allKeysUp struct{}

func (_ allKeysUp) KeyDown(_ uint8) bool { return false }

type randomlyDown struct{}

func (_ randomlyDown) KeyDown(_ uint8) bool { return rand.Intn(15) == 0 }

type always0 struct{}

func (_ always0) NextRand() uint8 { return 0 }
